<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约人" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入预约人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约手机" prop="userPhone">
        <el-input
            v-model="queryParams.userPhone"
            placeholder="请输入预约手机"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练" prop="spName">
        <el-input
            v-model="queryParams.spName"
            placeholder="请输入教练"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练手机" prop="spPhone">
        <el-input
            v-model="queryParams.spPhone"
            placeholder="请输入教练手机"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目" prop="aptItem">
        <el-input
            v-model="queryParams.aptItem"
            placeholder="请输入项目"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买单方式" prop="transWay">
        <el-select v-model="queryParams.transWay" placeholder="请选择买单方式" clearable>
          <el-option
              v-for="dict in t_apt_trans_way"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="当前节点" prop="flowStatus">
        <el-select v-model="queryParams.flowStatus" placeholder="请选择当前节点" clearable>
          <el-option
              v-for="dict in t_apt_flow_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
              v-for="dict in t_apt_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上下架" prop="isPublish">
        <el-select v-model="queryParams.isPublish" placeholder="请选择上下架" clearable>
          <el-option
              v-for="dict in t_publish_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺" prop="shopName">
        <el-input
            v-model="queryParams.shopName"
            placeholder="请输入店铺"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约地址" prop="addrDesc">
        <el-input
            v-model="queryParams.addrDesc"
            placeholder="请输入预约地址"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:tapt:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="预约人" align="center" prop="userName"/>
      <el-table-column label="预约手机" align="center" prop="userPhone"/>
      <el-table-column label="项目" align="center" prop="aptItem"/>
      <el-table-column label="买单方式" align="center" prop="transWay">
        <template #default="scope">
          <dict-tag :options="t_apt_trans_way" :value="scope.row.transWay" :showValue="false"/>
        </template>
      </el-table-column>
      <el-table-column label="当前节点" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="t_apt_flow_status" :value="scope.row.flowStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="t_apt_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="上下架" align="center">
        <template #default="scope">
          <dict-tag :options="t_publish_type" :value="scope.row.isPublish" v-if="scope.row.quantity !== 1"/>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="aptTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.aptTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约地址" width="300" align="center" prop="addrDesc"/>
      <el-table-column label="店铺" align="center" prop="shopName"/>
      <el-table-column label="教练" align="center" prop="spName"/>
      <el-table-column label="教练手机" align="center" prop="spPhone"/>
      <el-table-column label="服务时长" align="center" prop="serviceTime">
        <template #default="scope">
          <dict-tag :options="t_sp_service_time" :value="scope.row.serviceTime"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="350" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)"
                     v-hasPermi="['system:tapt:query']">查看
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     :class="{'but-disabled': !(scope.row.status === '04')}"
                     v-hasPermi="['system:tapt:remove']">删除
          </el-button>
          <el-button link type="primary" icon="Finished" @click="handlePublish(scope.row)"
                     :class="{'but-disabled': !(scope.row.quantity !== 1 && scope.row.isPublish === 0)}"
                     v-if="scope.row.quantity !== 1 && scope.row.isPublish === 0"
                     v-hasPermi="['system:tapt:publish']">上架
          </el-button>
          <el-button link type="primary" icon="DocumentDelete" @click="handleCancelPublish(scope.row)"
                     :class="{'but-disabled': !(scope.row.quantity !== 1 && scope.row.isPublish === 1)}"
                     v-if="scope.row.quantity !== 1 && scope.row.isPublish === 1"
                     v-hasPermi="['system:tapt:publish']">下架
          </el-button>
          <el-button link type="primary" icon="View" @click="handleAccept(scope.row)"
                     v-if="scope.row.status !== '04' && (scope.row.flowStatus === '24')"
                     v-hasPermi="['system:tapt:accept']">接单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="taptRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="接单意见" prop="auditStatus">
              <el-radio-group v-model="form.auditStatus">
                <el-radio
                    v-for="dict in t_audit"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="接单原因" prop="statusReason" :required="form.auditStatus==='03'">
              <el-input v-model="form.statusReason" placeholder="请输入接单原因"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="auditSubmitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="预约详情" v-model="viewOpen" width="1000px" append-to-body>
      <el-form ref="tplayRef" :model="form" :rules="rules" label-width="80px">
        <div class="cust-title">当前节点</div>
        <el-steps align-center>
          <el-step v-for="(item, index) in form.stepList" :key="index" :title="item.title"
                   :status="item.status"></el-step>
        </el-steps>
        <el-form-item label-width="auto">
          {{ form.spAcceptReason }} {{ form.acceptReason }}
        </el-form-item>
        <el-divider/>
        <div class="cust-title">邀约信息</div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预约人" prop="userName" label-width="auto">
              {{ form.userName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约手机" prop="userPhone" label-width="auto">
              {{ form.userPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目" prop="aptItem" label-width="auto">
              {{ form.aptItem }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="买单方式" prop="transWay" label-width="auto">
              <template #default="scope">
                <dict-tag :options="t_apt_trans_way" :value="form.transWay" :showValue="false"/>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预约时间" prop="aptTime" label-width="auto">
              {{ parseTime(form.aptTime, '{y}-{m}-{d} {h}:{i}') }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="店铺" prop="shopName" label-width="auto">
              {{ form.shopName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="教练" prop="spName" label-width="auto">
              {{ form.spName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教练手机" prop="spPhone" label-width="auto">
              {{ form.spPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider/>
        <div class="cust-title">已报名</div>
        <el-row>
          <el-col :span="5" v-for="(item,index) in form.joinUserList" :key="item.userId">
            <div class="cust-div-container">
              <image-preview :src="item.userAvatar" :width="50" :height="50"/>
            </div>
            <div class="cust-div-container">{{ item.userName }}/{{ item.userPhone }}</div>
            <!--            <div class="cust-joinOrdDesc">待核销</div>-->
            <div class="cust-joinOrdDesc">{{ item.joinOrdDesc }}</div>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Tapt">
import {listTapt, getTapt, delTapt, addTapt, updateTapt, acceptAptOpt, updatePublish} from "@/api/system/tapt";
import {ref} from "vue";
import useUserStore from "@/store/modules/user";

const {proxy} = getCurrentInstance();
const {
  t_sp_service_time,
  t_apt_trans_way,
  t_apt_status,
  t_publish_type,
  t_apt_flow_status, t_audit
} = proxy.useDict('t_sp_service_time', 't_apt_trans_way', 't_apt_status', 't_publish_type', 't_apt_flow_status', 't_audit');

const aptList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const viewOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    aptCode: undefined,
    aptDesc: undefined,
    type: undefined,
    spId: undefined,
    serviceTime: undefined,
    price: undefined,
    quantity: undefined,
    shopId: undefined,
    aptTime: undefined,
    aptItem: undefined,
    transWay: undefined,
    instDcl: undefined,
    expTime: undefined,
    isPublish: undefined,
    prodId: undefined,
    userId: undefined,
    status: undefined,
    statusReason: undefined,
  },
  rules: {
    auditStatus: [
      {required: true, message: "审核意见不能为空", trigger: "blur"}
    ],
    statusReason: [
      {required: true, message: "审核原因不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询预约列表 */
function getList() {
  loading.value = true;
  listTapt(queryParams.value).then(response => {
    aptList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    aptId: undefined,
    aptCode: undefined,
    aptDesc: undefined,
    type: undefined,
    spId: undefined,
    serviceTime: undefined,
    price: undefined,
    quantity: undefined,
    shopId: undefined,
    aptTime: undefined,
    aptItem: undefined,
    transWay: undefined,
    instDcl: undefined,
    expTime: undefined,
    isPublish: undefined,
    prodId: undefined,
    userId: undefined,
    status: undefined,
    auditStatus: undefined,
    statusReason: undefined,
    ext1: undefined,
    ext2: undefined,
    ext3: undefined,
    state: undefined,
    createTime: undefined,
    creator: undefined,
    modifyTime: undefined,
    modifier: undefined
  };
  proxy.resetForm("aptRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.aptId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleView(row) {
  const _aptIds = row.aptId || ids.value;
  getTapt(_aptIds).then(response => {
    form.value = response.data;
    viewOpen.value = true;
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _aptIds = row.aptId || ids.value;
  proxy.$modal.confirm('是否确认删除预约编号为"' + _aptIds + '"的数据项？').then(function () {
    return delTapt(_aptIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

function handlePublish(row) {
  const _aptId = row.aptId
  proxy.$modal.confirm('是否确认上架预约，编号为"' + _aptId + '"的数据项？').then(function () {
    row.isPublish = 1;
    return updatePublish(row);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("上架成功");
  }).catch(() => {
  });
}

function handleCancelPublish(row) {
  const _aptId = row.aptId
  proxy.$modal.confirm('是否确认下架预约，编号为"' + _aptId + '"的数据项？').then(function () {
    row.isPublish = 0;
    return updatePublish(row);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("下架成功");
  }).catch(() => {
  });
}

function handleAccept(row) {
  reset();
  form.value = row;
  form.value.statusReason = '';
  if (form.value.flowStatus == '24') {
    title.value = "教练接单";
  } else if (form.value.flowStatus == '25') {
    title.value = "商家接单";
  }
  open.value = true;
}

function auditSubmitForm(row) {
  proxy.$refs["taptRef"].validate(valid => {
    if (valid) {
      if (form.value.aptId != null) {
        let varAudit = -1;
        if (form.value.auditStatus == '02') {
          varAudit = 1;
        } else if (form.value.auditStatus == '03') {
          varAudit = 0;
        }
        acceptAptOpt({
          "aptId": form.value.aptId,
          "audit": varAudit,
          "auditReason": form.value.statusReason,
          "loginUserId": useUserStore().userId,
          "userType": useUserStore().userType
        }).then(response => {
          proxy.$modal.msgSuccess("接单成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/tapt/export', {
    ...queryParams.value
  }, `apt_${new Date().getTime()}.xlsx`)
}

onActivated(() => {
  getList();
})

getList();
</script>

<style lang='scss' scoped>
.cust-title {
  font-size: 20px;
  font-weight: 500;
}

.cust-joinOrdDesc {
  color: #801700;
  text-align: center;
}

.cust-div-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>